/**
 * LogbackUtils
 *
 * @author Chris Pratt
 *
 * 7/28/2011
 */
package com.anodyzed.onyx.log.logback;

import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.StackTraceElementProxy;

public class LogbackUtils {

  /**
   * Private Constructor
   */
  private LogbackUtils () {
  } //LogbackUtils

  /**
   * Turn the Throwable Proxy back into a Throwable for the Data Collection 
   * Service 
   *
   * @param proxy The Logback Throwable Proxy
   * @return The Java Throwable
   */
  public static Throwable toThrowable (IThrowableProxy proxy) {
    if(proxy != null) {
      Throwable t = new Exception(proxy.getClassName() + ": " + proxy.getMessage());
      int i = 0;
      StackTraceElementProxy[] proxies = proxy.getStackTraceElementProxyArray();
      StackTraceElement[] stack = new StackTraceElement[proxies.length];
      for(StackTraceElementProxy element : proxies) {
        stack[i] = element.getStackTraceElement();
      }
      t.setStackTrace(stack);
      t.initCause(toThrowable(proxy.getCause()));
      return t;
    }
    return null;
  } //toThrowable

} //*LogbackUtils
